package com.book.controller;


import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.book.entity.Order;
import com.book.service.OrderItemService;
import com.book.service.OrderService;
import com.book.utils.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderItemService orderitemService;
	private String showPath="/resource/showImg?path=";
	@RequestMapping("/list")
	public ModelAndView list(Order order, HttpServletRequest request, HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		String orderBy=" state ";
		Page orders=new Page();
		orders.setSize(getPageSize(request));
		orders.setCurrent(getPageNum(request));
		Wrapper<Order> con=new Condition();
		Page page = orderService.selectPage(orders, con);
		mav.addObject("orders", page);
		mav.setViewName("orders/list");
		return mav;
	}
	@RequestMapping("/admin/list")
	public ModelAndView adminList(Order order, HttpServletRequest request, HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		if(SecurityUtil.getCurrentUser().getUserType().equals("admin")){
			mav.setViewName("admin/orders/list");
		}else{
			mav.addObject("errorMsg","权限不足，只有管理员才能访问此页面");
			mav.setViewName( "common/500");
		}
		return mav;
	}
	@RequestMapping("/save")
	@ResponseBody
	public Map<String,Object> save(Order order, HttpServletRequest request, HttpServletResponse response){
		Map<String,Object> result=new HashMap<String, Object>();
		try{
			orderService.insert(order);
			msg.setSuccessMsg("下单成功!");
			result.put("msg", msg);
			return result;
		}catch(Exception e){
			msg.setErrorMsg("下单失败!");
			result.put("msg", msg);
			return result;
		}
	}
	@RequestMapping("/get")
	public ModelAndView get(HttpServletRequest request, HttpServletResponse response, ModelMap m){
		ModelAndView mav=new ModelAndView();
		String id=get(request, "id");
//		String showImgUrl=request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+request.getContextPath()+showPath;
		String showImgUrl="";
		mav.addObject("order", orderService.selectById(id));
		mav.addObject("showImgUrl", showImgUrl);
		mav.setViewName("orders/detail");
		return mav;
	}
	@RequestMapping("/to_edit")
	public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response, ModelMap m){
		ModelAndView mav=new ModelAndView();
		if(SecurityUtil.getCurrentRole()!=null &&"admin".equals(SecurityUtil.getCurrentRole().getName())){
			String oid=get(request, "oid");
//			String showImgUrl=request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+request.getContextPath()+showPath;
			String showImgUrl="";
			mav.addObject("order", orderService.selectById(oid));
			mav.addObject("showImgUrl", showImgUrl);
			mav.setViewName( "admin/orders/edit");
		}else{
			mav.addObject("errorMsg","权限不足，只有管理员才能访问此页面");
			mav.setViewName("common/500");
		}
		return mav;
	}
	@RequestMapping("/remove")
	@ResponseBody
	public Map<String,Object> remove(HttpServletRequest request, HttpServletResponse response, ModelMap m){
		Map<String,Object> resultMap=new HashMap<String, Object>();
			String oid=get(request, "oid");
			try{
				orderService.deleteById(oid);
				msg.setSuccessMsg("删除成功");
			}catch(Exception e){
				msg.setErrorMsg("删除成功");
			}
			resultMap.put("msg", msg);
			return resultMap;
	}
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String,Object> edit(Order order, HttpServletRequest request, HttpServletResponse response){
		Map<String,Object> resultMap=new HashMap<String, Object>();
		try{
			orderService.updateAllColumnById(order);
			if(StringUtils.isNotEmpty(order.getState())){
				if(order.getState().equals("1")){
					msg.setSuccessMsg("付款成功");
				}else if(order.getState().equals("2")){
					msg.setSuccessMsg("发货成功");
				}else if(order.getState().equals("4")){
				msg.setSuccessMsg("确认收货成功！欢迎继续购物");
				}else if(order.getState().equals("3")){
					msg.setSuccessMsg("取消成功,我们将继续努力提升服务质量");
				}
				
			}
		}catch(Exception e){
			msg.setErrorMsg("系统繁忙，请稍后再试");
		}
			resultMap.put("msg", msg);
			return resultMap;
	}


	@RequestMapping("/getPager")  //待解决
	@ResponseBody
	public ModelAndView getPager(Order order, HttpServletRequest request, HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		String type=get(request, "type");
		if(StringUtils.isEmpty(type)){
			type="0";
		}
		String entitySQL="";
		if("1".equals(type)){
			entitySQL=" state !='1' and state !='0'";
		}else if("0".equals(type)){
			order.setState("1");
		}
		
		return mav;
	}
}
